/**
 * 面试窄列表
 */
import React from 'react';
import { Table } from 'antd';
import { TableFilter } from '@/components';
import { getDisplayText } from '@/utils/dataDictUtil';
import { connect } from 'umi';
import { useInterviewRoundList } from '../hooks';
import {
    renderShortDate,
    renderInterviewStatus,
    renderNameWithReadFlagTitle,
    renderNameWithReadFlag,
    renderInterviewRecordOperate
} from '../renderUtil';
import { INTERVIEW_STATUS, SEX } from '../dictionary';

const InterviewCommonTable = ({
    loading,
    dataSource,
    condition,
    onJumpDetail,
    onRefresh,
    sortOrders,
    isPrincipal,
    isLauncher,
    user,
    ...rest
}) => {
    const { currentUser = {} } = user;
    // 面试轮次
    const { data: interviewRoundList, loading: interviewRoundListLoading } = useInterviewRoundList();
    const columns = [
        {
            title: renderNameWithReadFlagTitle('姓名'),
            dataIndex: 'readFlag',
            render: (value, record) => {
                const { talentName = '--', showReadFlag, hasBlackList } = record;
                return renderNameWithReadFlag(talentName, value, showReadFlag, hasBlackList);
            }
        },
        {
            title: '性别',
            dataIndex: 'talentSex',
            width: 80,
            filteredValue: condition.talentSex ? [condition.talentSex] : [],
            filters: SEX,
            filterMultiple: false,
            filterDropdown: filterProps => <TableFilter {...filterProps} />,
            render: sex => getDisplayText(SEX, sex)
        },
        {
            title: '面试官',
            dataIndex: 'interviewerEmployeeName',
            width: 180,
            render: val => {
                const split = '、';
                let ret = val;
                if (!ret) {
                    ret = '--';
                } else {
                    const arr = val.split(split);
                    const num = arr.length;
                    if (num > 2) {
                        ret = `${arr.splice(0, 2).join(split)}等${num}人`;
                    }
                }
                return ret;
            }
        },
        {
            title: '面试时间',
            dataIndex: 'interviewStartTime',
            sortOrder: sortOrders?.interviewStartTime || false,
            render: val => renderShortDate(val, 'MM-DD HH:mm')
        },
        {
            title: '面试轮次',
            dataIndex: 'interviewRoundName',
            filteredValue: condition.interviewRound ? [condition.interviewRound] : [],
            filters: interviewRoundList,
            filterMultiple: false,
            filterDropdown: filterProps => <TableFilter {...filterProps} />
        },
        {
            title: '状态',
            dataIndex: 'interviewStatus',
            width: 80,
            filteredValue: condition.interviewStatus ? [condition.interviewStatus] : [],
            filters: INTERVIEW_STATUS,
            filterMultiple: false,
            filterDropdown: filterProps => <TableFilter {...filterProps} />,
            render: (val, record) => renderInterviewStatus(val, record.interviewStatusHover)
        },
        {
            title: '操作',
            dataIndex: 'action',
            width: 280,
            onCell: () => ({
                onClick: e => {
                    e.stopPropagation();
                }
            }),
            render: (_, record) =>
                renderInterviewRecordOperate(record, onRefresh, isPrincipal, currentUser?.employeeCode, isLauncher)
        }
    ];
    return (
        <>
            <Table
                {...rest}
                rowKey="recruitTalentMatcherId"
                bordered={false}
                columns={columns}
                loading={loading || interviewRoundListLoading}
                dataSource={dataSource}
                pagination={false}
                onRow={record => ({
                    onClick: () => {
                        const { recruitTalentMatcherId, candidateId } = record;
                        if (onJumpDetail) onJumpDetail(candidateId, recruitTalentMatcherId);
                    }
                })}
            />
        </>
    );
};
export default connect(({ user }) => ({ user }))(InterviewCommonTable);
